//
//  HttpTool.m
//  2131
//
//  Created by 健健 on 2018/2/24.
//  Copyright © 2018年 123. All rights reserved.
//

#import "HttpTool.h"
#import <AFNetworking.h>


@implementation HttpTool

+(void)getApi:(NSString *)api params:(NSDictionary *)params success:(NetSuccess)success failure:(NetFailure)failure {
    
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 10.0;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/javascript", nil];
    NSString *url = [NSString stringWithFormat:@"%@%@",@"",api];
    if ([url hasPrefix:@"http://"]) {
        url = api;
    }
    [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id jsonObject = nil;
        NSError *error = nil;
        jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        success(jsonObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error.description);
        }
    }];
    
}


+ (void)postApi:(NSString *)api params:(NSDictionary *)params success:(NetSuccess)success failure:(NetFailure)failure isLoding:(BOOL)isLoding {
    if (isLoding) {
        [ProgressHUD showLoading];
    }
    AFHTTPSessionManager *manager1 =[AFHTTPSessionManager manager];
    manager1.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager1.requestSerializer.timeoutInterval = 10.0;
    manager1.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/javascript", nil];

    [manager1 POST:api parameters:[self getNewParamsFromParams:params] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (isLoding) {
            [ProgressHUD dismiss];
        }
        id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if (obj) {
            if ([obj[@"status"] integerValue] == 1) {
                success(obj);
            }else {
                failure(obj[@"msg"]);
            }
        }else {
            NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
            if (failure) {
                failure(result);
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (failure) {
            failure(error.description);
        }
    }];
}



+ (NSDictionary *)getNewParamsFromParams:(NSDictionary *)params {
    NSMutableDictionary *newParams = [[NSMutableDictionary alloc] initWithDictionary:params];
    NSString *time = [self getTime];
    newParams[@"data"] = time;
    NSString *token = [NSString stringWithFormat:@"%@2016",time];
    newParams[@"token"] = token;
    newParams[@"device"] = @"1";
    return newParams;
}

//获取时间戳
+ (NSString *)getTime
{
    NSDate *datenow = [NSDate date];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    return timeSp;
}
@end
